//用快排思想找第k小数
function searching(arr, index) {
    let guard = arr[0];
    let i = 0, j = arr.length - 1;
    while (i < j) {
        while (arr[j] > guard && i < j) j--;
        arr[i] = arr[j];
        while (arr[i] <= guard && i < j) i++;
        arr[j] = arr[i];
    }
    arr[i] = guard;
    if (i+1 === index) return guard;
    else if (i+1 > index) {
        return searching(arr.slice(0, i), index);
    } else {
        return searching(  arr.slice(i + 1), index-i-1);
    }
}

console.log(searching([1, 2, 2, 3, 10, 5, 3, 8], 4));

